package com.huitone.smspfm.oldclient.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huitone.smspfm.oldclient.logger.ExtLogger;

import java.util.ArrayList;
import java.util.List;

import com.bin.common.utils.StringUtils;
import com.huitone.smspfm.context.utils.ContextPropertiesUtils;
import com.huitone.smspfm.context.utils.ContextUtils;
import com.huitone.smspfm.oldclient.model.DbSmsConf;

/**   
* @Title AppStaticConfigs.java 
* @Package com.huitone.smspfm.oldclient.app 
* @Description TODO(用一句话描述该文件做什么) 
* @author ybliang   
* @date 2017年7月18日 下午6:07:17 
* @version V1.0   
*/
public class AppStaticConfigs {
	
	public static ExtLogger logger = ExtLogger.create(AppStaticConfigs.class);
	public static boolean isWindows = false;
	static {
		isWindows = System.getProperty("os.name").indexOf("Window") >= 0;
		ContextUtils.getInstance();
	}
	
	/**
	 * 客户端服务接受的最大socket连接数
	 */
	public static int maxSocketSize = 5;
	
	/**
	 * 客户端服务端信息处理类的处理线程池大小
	 */
	public static int shExcutorPoolSize = 1;
	
	/**
	 * 提交响应消息处理类的处理线程池大小
	 */
	public static int chExcutorPoolSize = 1;
	
	/**
	 * 客户端服务监听端口
	 */
	public static int clientServerPort = 9901;
	
	/**
	 * 短信平台绑定客户端服务的用户名
	 */
	public static String clientServerLoginName = null;
	
	/**
	 * 短信平台绑定客户端服务的密码
	 */
	public static String clientServerLoginPass = null;
	
	/**
	 * 新平台业务端编码
	 */
	public static String srvNode = null;
	
	/**
	 * 新平台帐号对应的业务类型代码
	 */
	public static String serviceCode = null;
	
	/**
	 * 新平台序列号编码规则
	 */
	public static int codeRule = 2000001;
	
	/**
	 * 是否是多帐号模式针对1.38服务器
	 */
	public static boolean isMultiAccount = false;
	
	/**
	 * 是否使用旧的业务代码(短信类型)
	 */
	public static boolean isUseOldServiceCode = false;
	
	/**
	 * 是否备份已读取的短信文件
	 */
	public static boolean backupSmsFiles = false;
	
	/**
	 * 平台接口Ip地址
	 */
	public static String serverIp = null;
	
	/**
	 * 平台接口服务端口号
	 */
	public static Integer serverPort = null;
	
	/**
	 * 平台接口登录名
	 */
	public static String serverLoginName = null;
	
	/**
	 * 平台接口登录密码
	 */
	public static String serverLoginPass = null;
	
	/**
	 * 短信发送线程池大小
	 */
	public static int msgSendPoolSize = 1;
	
	/**
	 * 短信发送速率最大值
	 */
	public static int msgSendMaxSize = 100;
	
	/**
	 * 下行短信目录
	 */
	public static String msgFileDir = null;
	
	/**
	 * 短信文件名匹配模式
	 */
	public static String msgFileNameRegex = null;
	
	// 加载系统参数
	
	/**
	 * 等待合并的长短信集合文件, 主要是在停止应用的时候保存这些未合并完成的长短信
	 */
	public static String mtGroupTmpStoreFile = null;
	
	/**
	 * 等待发送响应返回的短信集合文件, 主要是在停止应用的时候保存这些未收到提交响应的短信
	 */
	public static String mtTmpStoreFile = null;
	
	/**
	 * 提取发送端口号的正则表达式，多个正则表达式以逗号分割，匹配优先级从左到右降低，每个正则表达式需要提取的组由rspSpExtractRegexsMappingGroup确定
	 */
	public static List<String> rspSpExtractRegexs = new ArrayList<String>();
	public static List<Integer> rspSpExtractRegexsMappingGroup = new ArrayList<Integer>();
	
	/**
	 * 提取发送端口号的正则表达式，多个正则表达式以逗号分割，匹配优先级从左到右降低，每个正则表达式需要提取的组由rptSpExtractRegexsMappingGroup确定
	 */
	public static List<String> rptSpExtractRegexs = new ArrayList<String>();
	public static List<Integer> rptSpExtractRegexsMappingGroup = new ArrayList<Integer>();
	
	/**
	 * 提取发送端口号的正则表达式，多个正则表达式以逗号分割，匹配优先级从左到右降低，每个正则表达式需要提取的组由dlvSpExtractRegexsMappingGroup确定
	 */
	public static List<String> dlvSpExtractRegexs = new ArrayList<String>();
	public static List<Integer> dlvSpExtractRegexsMappingGroup = new ArrayList<Integer>();

	/**
	 * 响应消息文件条消息需要打印的元素
	 */
	public static List<String> rspRowElements = new ArrayList<String>();
	
	/**
	 * 状态报告消息文件条消息需要打印的元素
	 */
	public static List<String> rptRowElements = new ArrayList<String>();
	
	/**
	 * 上行短信消息文件每条消息需要打印的元素
	 */
	public static List<String> dlvRowElements = new ArrayList<String>();
	
	/**
	 * 下行短信发送表(按照优先级顺序)
	 */
//	public static List<String> smsSendTables = new ArrayList<String>();

	/**
	 * 下行短信发送表配置信息(按照优先级顺序)
	 */
	public static List<DbSmsConf> dbSmsConfList = new ArrayList<>();
	
	/**
	 * 线程一次处理下行短信的限制
	 */
	public static Integer smsSendLimit = null;

	
	/**
	 * 上行短信webservice的url
	 */
	public static String smsRecvWebServiceUrl = null;
	
	
	
	/**
	 * 旧短信序号是否重复过滤
	 */
	public static boolean repeatseqsFilterOn = true;
	
	/**
	 * 短信文件的来源方式  file,db
	 */
	public static SmsSourceType smsSourceType = SmsSourceType.FILE;
	
	/**
	 * 重新加载Properties配置文件
	 */
	public static void reload() {
		ContextPropertiesUtils.reload();
		init();
	}
	
	/**
	 * 初始化静态参数
	 */
	public static void init() {
		initSystemParams();
		initProperties();
	}
	
	private static void initSystemParams() {
		
		mtTmpStoreFile = System.getProperty("MtTmpStore");
		
		mtGroupTmpStoreFile = System.getProperty("MtGroupTmpStore");
	}


	/**
	 * 初始化属性值
	 */
	private static void initProperties() {
		
		srvNode = ContextPropertiesUtils.getProperty("smspfm.client.srvNode");
		codeRule = StringUtils.toInteger(ContextPropertiesUtils.getProperty("smspfm.client.codeRule"));
		serviceCode = ContextPropertiesUtils.getProperty("smspfm.client.serviceCode");
		isMultiAccount = StringUtils.equals(ContextPropertiesUtils.getProperty("smspfm.client.multi.account"), "true");
		isUseOldServiceCode = StringUtils.equals(ContextPropertiesUtils.getProperty("smspfm.client.useold.service.code"), "true");
		backupSmsFiles = StringUtils.equals(ContextPropertiesUtils.getProperty("smspfm.client.backup.sms.files"), "true");
		clientServerPort = StringUtils.toInteger(ContextPropertiesUtils.getProperty("smspfm.client.server.port"), 9901, true);
		clientServerLoginName = StringUtils.toString(ContextPropertiesUtils.getProperty("smspfm.client.server.loginName"));
		clientServerLoginPass = StringUtils.toString(ContextPropertiesUtils.getProperty("smspfm.client.server.loginPassword"));
		shExcutorPoolSize = StringUtils.toInteger(ContextPropertiesUtils.getProperty("smspfm.client.server.handler.poolsize"), 1, true);
		chExcutorPoolSize = StringUtils.toInteger(ContextPropertiesUtils.getProperty("smspfm.client.handler.poolsize"), 1, true);
		maxSocketSize = StringUtils.toInteger(ContextPropertiesUtils.getProperty("smspfm.client.server.maxActive"), 5, true);
		serverIp = ContextPropertiesUtils.getProperty("smspfm.server.host");
		serverPort = StringUtils.toInteger(ContextPropertiesUtils.getProperty("smspfm.server.port"));
		serverLoginName = ContextPropertiesUtils.getProperty("smspfm.client.loginName");
		serverLoginPass = StringUtils.toString(ContextPropertiesUtils.getProperty("smspfm.client.loginPassword"));
		msgSendPoolSize = StringUtils.toInteger(ContextPropertiesUtils.getProperty("smspfm.client.send.poolSize"),1,true);
		msgFileDir = ContextPropertiesUtils.getProperty("smspfm.client.file.dir.mt");
		msgFileNameRegex = ContextPropertiesUtils.getProperty("smspfm.client.file.name.regex");
		msgSendMaxSize = StringUtils.toInteger(ContextPropertiesUtils.getProperty("smspfm.client.send.maxSize"),100,true);
		
		rspSpExtractRegexs = StringUtils.splitToStringList(ContextPropertiesUtils.getProperty("resp.file.sendport.extract.regex"), ",");
		rspSpExtractRegexsMappingGroup = StringUtils.splitToIntegerList(ContextPropertiesUtils.getProperty("resp.file.sendport.extract.group"), ",");
		int rspInterval = rspSpExtractRegexs.size() - rspSpExtractRegexsMappingGroup.size();
		for (int i = 0 ; i < rspInterval ; i ++) {
			rspSpExtractRegexsMappingGroup.add(0);
		}
		
		rptSpExtractRegexs = StringUtils.splitToList(ContextPropertiesUtils.getProperty("report.file.sendport.extract.regex"), ",");
		rptSpExtractRegexsMappingGroup = StringUtils.splitToIntegerList(ContextPropertiesUtils.getProperty("report.file.sendport.extract.group"), ",");
		int rptInterval = rptSpExtractRegexs.size() - rptSpExtractRegexsMappingGroup.size();
		for (int i = 0 ; i < rptInterval ; i ++) {
			rptSpExtractRegexsMappingGroup.add(0);
		}
		
		dlvSpExtractRegexs = StringUtils.splitToList(ContextPropertiesUtils.getProperty("deliver.file.sendport.extract.regex"), ",");
		dlvSpExtractRegexsMappingGroup = StringUtils.splitToIntegerList(ContextPropertiesUtils.getProperty("deliver.file.sendport.extract.group"), ",");
		int dlvInterval = dlvSpExtractRegexs.size() - dlvSpExtractRegexsMappingGroup.size();
		for (int i = 0 ; i < dlvInterval ; i ++) {
			dlvSpExtractRegexsMappingGroup.add(0);
		}

		// DbSmsConf table配置
		String configStr = ContextPropertiesUtils.getProperty("smspfm.client.smsend.tables");
		List<DbSmsConf> dbSmsConfListTmp = new ArrayList<>();
		try {
			JSONArray jsonArray = JSON.parseArray(configStr);
			if (jsonArray != null && !jsonArray.isEmpty()) {
				for (int i = 0; i < jsonArray.size(); i++) {
					JSONObject jsonObject = jsonArray.getJSONObject(i);
					DbSmsConf dbSmsConf = DbSmsConf.parseDbSmsConf(jsonObject.toJSONString());
					if (dbSmsConf != null) {
						dbSmsConfListTmp.add(dbSmsConf);
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error("DbSmsConf table配置 init失败！");
		}
		if (dbSmsConfListTmp != null && !dbSmsConfListTmp.isEmpty()){
			dbSmsConfList = dbSmsConfListTmp;
		}

		rspRowElements = StringUtils.splitToStringList(ContextPropertiesUtils.getProperty("resp.file.row.elements.select"), ",");
		rptRowElements = StringUtils.splitToStringList(ContextPropertiesUtils.getProperty("report.file.row.elements.select"), ",");
		dlvRowElements = StringUtils.splitToStringList(ContextPropertiesUtils.getProperty("deliver.file.row.elements.select"), ",");

//		smsSendTables = StringUtils.splitToStringList(ContextPropertiesUtils.getProperty("smspfm.client.smsend.tables"), ",");
		smsSendLimit =  StringUtils.toInteger(ContextPropertiesUtils.getProperty("smspfm.client.smsend.limit"), 1000, true);
		smsRecvWebServiceUrl = StringUtils.toString(ContextPropertiesUtils.getProperty("smspfm.client.smrecv.webservice.url"));
		
		SmsSourceType tmpSmsSourceType = SmsSourceType.valueOfSource(StringUtils.toString(ContextPropertiesUtils.getProperty("smspfm.client.sms.source")));
		
		if (tmpSmsSourceType != null){
			smsSourceType = tmpSmsSourceType;
		}
	
	}
}
